/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            //loading();
            this.fetch();
            this.event();
        },
        fetch: function () {
            var html = template('add-tpl');
            $("#main-view").html(html);
        },
        event: function () {
            this.data = {};
            var _this = this;
            //日期
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });
            $("#btn-search").on("click", function (e) {
                e.preventDefault();
                _this.select_goods(e);
            });
            //保存
            $("#btn_save").on("click", function () {
                _this.data.limit_num = $("input[name='limit_num']").val();
                _this.data.limit_price = $("input[name='limit_price']").val();

                _this.data.started_at = $("#started_at").val();
                _this.data.expired_at = $("#expired_at").val();
                if (_this.data.started_at == '' || _this.data.expired_at == '') {
                    Alert({msg: '时间不能为空', type: 'error'});
                    return false;
                }
                var goods_id = '';
                $(".goods_id").each(function () {
                    if ($(this).val()) {
                        goods_id = $(this).val();
                    }
                });
                if (goods_id == '') {
                    Alert({msg: '请选择要商品', type: 'error'});
                    return false;
                }
                _this.data.goods_id = goods_id;

                if (new Model({
                        url: _global.url.api + 'limitbuy',
                        data: _this.data
                    }).post()) {
                    location.href = '/site/limitbuy';
                }

            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },
        select_goods: function (e) {
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-items',
                url: _global.url.api + 'promotion_goods_search',
                condition: {keyword: $("#keyword").val()},
                model: {
                    event: {
                        '.item-bind:click': 'addgoods',
                        '.show-online:click': 'setshow'
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function () {
                                if (model.del()) {
                                    _this.collection.reload();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    addgoods: function (e) {
                        e.preventDefault();
                        var _this = this;
                        var id = this.data.id;
                        var vals = new Array();
                        $(".goods_id").each(function () {
                            if ($(this).val()) {
                                vals.push($(this).val());
                            }
                        });
                        if (vals.length > 0) {
                            $('.goods-item-del').click();//只能选择一个商品（特别增加代码）
                        }
                        var goods_id = ',' + vals.join(",") + ',';
                        if (goods_id.indexOf(',' + id + ',') != -1) {
                            Alert({msg: '请不要重复添加同一商品', type: 'error'});
                            return false;
                        }
                        $('#goods-item').append(template('goods-item-tpl', {
                            goods_id: this.data.id,
                            goods_name: this.data.name,
                            price: this.data.price
                        }));
                        $("#div_goods").show();
                        $('.goods-item-del').on('click', function (e) {
                            e.preventDefault();
                            $(this).parent().parent().remove();
                            $('.item-bind' + id).html('选择').removeClass('btn-success').addClass('btn-default');
                        });

                        $('.item-bind' + id).html('已选择').removeClass('btn-default').addClass('btn-success');
                    }

                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        }

    };

    new index();

});